<template>
    <uni-popup ref="loPopup" :type="type">
        <div class="lo_popup_container" :class="['pos_'+ type]">
            <div class="title">
                {{title}}
                <i class="icon-close" @click="close"></i>
            </div>
            <div class="slot">
                <slot></slot>
            </div>
            <div class="btns" v-if="showBtn">
                <div class="btn" v-if="showCancel" @click="cancel">{{cancelText}}</div>
                <div class="btn" @click="confirm">{{confirmText}}</div>
            </div>
        </div>
    </uni-popup>
</template>

<script>
export default {
    props: {
        title: {
            type: String,
            default: ''
        },
        type: {
            type: String,
            default: 'center'
        },
        showCancel: {
            type: Boolean,
            default: false,
        },
        cancelText: {
            type: String,
            default: '取消'
        },
        confirmText: {
            type: String,
            default: '确定'
        },
        showBtn: {
            type: Boolean,
            default: true,
        }
    },
    data() {
        return {
            promiseResolve: null,
        }
    },
    mounted() {
        // setTimeout(() => {
        //     this.$refs.loPopup.open()
        // }, 500)
    },
    methods: {
        open() {
            return new Promise((resolve, reject) => {
                this.promiseResolve = resolve
                this.$refs.loPopup.open()
            })
        },
        close() {
            this.$refs.loPopup.close()
        },
        cancel() {
            if(this.$listeners.cancel) {
                this.$emit('cancel')
            } else {
                this.promiseResolve({cancel: true})
                this.close()
            }
        },
        confirm() {
            if(this.$listeners.confirm) {
                this.$emit('confirm')
            } else {
                this.promiseResolve({confirm: true})
                this.close()
            }
        }
    }
}
</script>

<style lang="less" scoped>
.lo_popup_container{
    background: #fff;
    padding: 0 30rpx 30rpx;
    position: relative;
    .title{
        font-size: 34rpx;
        font-weight: bold;
        text-align: center;
        line-height: 80rpx;
        margin-bottom: 20rpx;
        color: #444;
        .icon-close{
            color: #ccc;
            font-size: 26rpx;
            width: 80rpx;
            height: 80rpx;
            right: 0;
            top: 0;
            position: absolute;
        }
    }
    &.pos_center{
        width: 660rpx;
        border-radius: 20rpx;
    }
    &.pos_bottom{
        width: 100%;
        border-radius: 30rpx 30rpx 0 0;
    }
    .slot{
        min-height: 100rpx;
    }
    .btns{
        width: 100%;
        height: 80rpx;
        display: flex;
        .btn{
            flex: 1;
            margin-right: 30rpx;
            .align(center);
            height: 100%;
            // background: #ddd;
            border-radius: 10rpx;
            border: 1px solid #ddd;
            &:last-child{
                margin-right: 0;
                border: 0;
                background: @mainColor;
                color: #fff;
            }
        }
    }
}
</style>